package com.hjb.toolbardemo.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.app_library.utils.LogC;
import com.hjb.toolbardemo.R;
import com.hjb.toolbardemo.adapter.HomeRecycleAdapter;
import com.hjb.toolbardemo.api.CarResponse;
import com.hjb.toolbardemo.base.BaseFragment;
import com.hjb.toolbardemo.bean.Car;
import com.hjb.toolbardemo.inter.OnRecyclerViewItemClickListener;
import com.hjb.toolbardemo.utils.OkHttpUtils;
import com.okhttpfinal.MyBaseHttpRequestCallback;
import com.widget.SuperSwipeRefreshLayout;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者 : hjb
 * 时间 : 2016/4/25.
 */
public class HomeTabFragment extends BaseFragment {


    @InjectView(R.id.listView)
    RecyclerView mRecyclerView;
    private HomeRecycleAdapter adapter;

    @Override
    public View initRootView() {
        rootView = mInflater.inflate(R.layout.frag_home, null);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void setViews() {
        getData();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new HomeRecycleAdapter(getActivity(), null);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setHasFixedSize(true);//如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
              LogC.e("-----");
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
                LogC.e("+++++");
                return true;
            }
        });
    }

    private void getData() {
        OkHttpUtils.listCar(new MyBaseHttpRequestCallback<CarResponse>() {
            @Override
            public void onRequestSuccess(CarResponse response) {
                List<Car> data = response.getData();
                adapter.append(data);
            }

            @Override
            public void onRequestFailure(CarResponse message) {

            }

        });
    }

    @Override
    public void initData() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
